import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { ProjectVO, ProjectForm, ProjectQuery } from '@/api/exam/project/types';
import { AnswerQuery } from '@/api/exam/answer/types';

const apiPrefix = '/exam/project/';

/**
 * 查询项目列表
 * @param query
 * @returns {*}
 */
export const pageProject = (query?: ProjectQuery): AxiosPromise<ProjectVO[]> => {
  return request({
    url: apiPrefix + 'page',
    method: 'get',
    params: query
  });
};

/**
 * 查询个人参与的项目列表
 * @param query
 * @returns {*}
 */
export const pagePartake = (query?: ProjectQuery): AxiosPromise<ProjectVO[]> => {
  return request({
    url: apiPrefix + 'partake',
    method: 'get',
    params: query
  });
};
/**
 * 根据用户ID查询项目列表
 * @param query
 * @returns {*}
 */
export const listProjectByUserId = (query?: AnswerQuery): AxiosPromise<ProjectVO[]> => {
  return request({
    url: apiPrefix + 'listByUserId',
    method: 'get',
    params: query
  });
};

// 获取项目列表
export function listProject(query?: ProjectQuery): AxiosPromise<ProjectVO[]> {
  return request({
    url: apiPrefix + 'list',
    method: 'get',
    params: query
  });
}

/**
 * 查询项目详细
 * @param projectId
 */
export const getProject = (projectId: string|number ): AxiosPromise<ProjectVO> => {
  return request({
    url: apiPrefix + projectId,
    method: 'get'
  });
};

/**
 * 新增项目
 * @param data
 */
export const addProject = (data: ProjectForm) => {
  return request({
    url: apiPrefix,
    method: 'post',
    data: data
  });
};

/**
 * 修改项目
 * @param data
 */
export const updateProject = (data: ProjectForm) => {
  return request({
    url: apiPrefix,
    method: 'put',
    data: data
  });
};
/**
 * 重命名
 * @param data
 */
export const renameProject = (data) => {
  return request({
    url: apiPrefix +'rename',
    method: 'put',
    data: data
  });
};
/**
 * 删除项目
 * @param projectId
 */
export const delProject = (projectId: string | number | Array<string | number>) => {
  return request({
    url: apiPrefix + projectId,
    method: 'delete'
  });
};

/*
* 发布项目
* */
export const publishProject = (id: string | number): AxiosPromise<ProjectVO> => {
  return request({
    url: apiPrefix + 'publish/' + id,
    method: 'put',
  });
};
/*取消发布*/
export const cancelPublishProject = (id: string | number): AxiosPromise<ProjectVO> => {
  return request({
    url: apiPrefix + 'cancelPublish/' + id,
    method: 'put'
  });
};

/*预览项目*/
export const previewProject = (id: string | number) => {
  return request({
    url: apiPrefix + 'preview/' + id,
    method: 'get'
  });
};

